from PyQt6.QtGui import *
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtNetwork import *
import json

from CheckInfo.TypeRecognize import *

WIDTH = 900
HEIGHT = 560

class UploadWidget(QWidget):

    client = QTcpSocket()
    def __init__(self):
        super().__init__()
        self.resize(WIDTH, HEIGHT)
        self.info = InfoWidget()

        self.layout = QVBoxLayout()

        self.mainArea = QScrollArea()
        self.mainArea.setWidgetResizable(True)
        self.mainArea.setWidget(self.info)

        self.layout.addWidget(self.mainArea)
        self.setLayout(self.layout)

    def setUsername(self, username):
        print("in up")
        self.info.setUsername(username)

    def setSocket(self, socket):
        self.info.setSocket(socket)


class InfoWidget(QWidget):

    filepathList = []
    userName = ""
    sendInfo = {}
    client = QTcpSocket()

    def __init__(self):
        super().__init__()

        self.layout = QVBoxLayout()

        self.fileButton = QPushButton("选择图片...")
        self.submitButton = QPushButton("提交")

        buttonLayout = QHBoxLayout()
        buttonLayout.addWidget(self.fileButton)
        buttonLayout.addWidget(self.submitButton)

        self.fileButton.clicked.connect(self.chooseImage)
        self.submitButton.clicked.connect(self.submit)

        self.imageWidget = QWidget()

        self.imageLayout = QVBoxLayout()
        self.imageWidget.setLayout(self.imageLayout)

        imageScroll = QScrollArea()
        imageScroll.setWidget(self.imageWidget)
        imageScroll.setWidgetResizable(True)

        self.layout.addWidget(imageScroll)
        self.layout.addLayout(buttonLayout)
        self.setLayout(self.layout)

        self.typeList = []

    def chooseImage(self):
        file_dialog = QFileDialog()
        file_dialog.setNameFilter("Images (*.png *.jpg *.jpeg *.bmp *.gif)")
        file_dialog.setViewMode(QFileDialog.ViewMode.List)
        if file_dialog.exec():
            file_paths = file_dialog.selectedFiles()
            for file_path in file_paths:
                self.filepathList.append(file_path)
                pixmap = QPixmap(file_path)
                image_label = QLabel()
                image_label.setPixmap(pixmap)
                self.imageLayout.addWidget(image_label)

    def submit(self):
        self.sendInfo["username:"] = self.userName
        self.sendInfo["picture:"] = []
        info = [{"message": "images"}, {"username": self.userName}]
        for path in self.filepathList:
            check = CommonOcr(path)
            print(check.recognize())
            temp = json.loads(check.recognize())
            image = QImage(path)
            buffer = QBuffer()
            buffer.open(QIODevice.OpenModeFlag.ReadWrite)
            image.save(buffer, "JPG")
            temp["image"] = buffer.data().toBase64().data().decode()
            info.append(json.dumps(temp))
        json_data = json.dumps(info)
        print(json_data)
        self.client.write(json_data.encode())
        print(self.typeList)

    def setUsername(self, username):
        self.userName = username

    def setSocket(self, socket):
        self.client = socket
